﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Net;
using System.Configuration;


namespace HRP.Utilidades
{
    public static class ClassEmail
    {
        private static string smptServer = ConfigurationManager.AppSettings["mailSmtpServer"];
        private static int port = Convert.ToInt32(ConfigurationManager.AppSettings["mailPort"]);
        private static string username = ConfigurationManager.AppSettings["mailUsername"];
        private static string password = ConfigurationManager.AppSettings["mailPassword"];

        public static int sendMail(string mailFrom, string mailTo, string mailBcc, string subject, string message)
        {
            int result = 0;
            try
            {
                //use using as try catch
                MailMessage msg = new MailMessage();
                msg.From = new MailAddress(mailFrom);
                string[] toAdd = mailTo.Split(';');
                foreach (string emailTo in toAdd)
                    msg.To.Add(emailTo);

                if (!string.IsNullOrEmpty(mailBcc))
                {
                    string[] bccAdd = mailBcc.Split(';');
                    foreach (string emailBcc in bccAdd)
                        msg.Bcc.Add(new MailAddress(emailBcc));
                }
                msg.Subject = subject;
                msg.Body = message;
                msg.IsBodyHtml = true;

                SmtpClient smtp = new SmtpClient(smptServer);
                smtp.EnableSsl = true;
                smtp.Port = port;
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.Credentials = new NetworkCredential(username, password);
                smtp.Send(msg);
                result = 1;
            }
            catch (Exception)
            {
            }

            return result;

        }
    }
}